package com.charge;

import java.util.concurrent.Exchanger;

public class ExgrDemo {  
    public static void main(String args[]) {  
          Exchanger<String> exgr = new Exchanger<String>();  

         // new UseString(exgr);  
          new MakeString(exgr); 
          new UseString2(exgr);
        }  
      }  

      class MakeString implements Runnable {  
        Exchanger<String> ex;  

        String str;  

        MakeString(Exchanger<String> c) {  
          ex = c;  
          str = new String();  

          new Thread(this).start();  
        }  

        public void run() {  
          char ch = 'A';  
          for (int i = 0; i < 4; i++) {  
            for (int j = 0; j < 5; j++)  
              str += (char) ch++;  

            try {  
            	 System.out.println("----str111--->"+str);
              str = ex.exchange(str);  
              System.out.println("----get111--->"+str);
            } catch (InterruptedException exc) {  
              System.out.println(exc);  
            }  
          }  
        }  
      }  

      class UseString implements Runnable {  
        Exchanger<String> ex;  

        String str;  

        UseString(Exchanger<String> c) {  
          ex = c;  
          new Thread(this).start();  
        }  

        public void run() {  

          for (int i = 0; i < 3; i++) {  
            try {  
              str = ex.exchange(new String());  
              System.out.println("Got:11 " + str);  
            } catch (InterruptedException exc) {  
              System.out.println(exc);  
            }  
          }  
        }  
      }  
      
      class UseString2 implements Runnable {  
          Exchanger ex;  

          String str;  

          UseString2(Exchanger<String> c) {  
            ex = c;  
            new Thread(this).start();  
          }  

          public void run() {  

            for (int i = 0; i < 3; i++) {  
              try {  
                str = (String)ex.exchange("aaaaa");  
                System.out.println("Got:22 " + str);  
              } catch (InterruptedException exc) {  
                System.out.println(exc);  
              }  
            }  
          }  
        } 
class MyTest{
	public MyTest(){
		for(long i=0;i<100000000L;i++){
			
		}
	}
	public String toSring(){
		return "aaa";
	}
}